import React, { Component } from "react";
import { Button, Text, View } from "react-native";
import { NativeEventEmitter, NativeModules } from 'react-native';

class DeviceMessage extends Component {
  state = { isHungry: true, name: "tom" };

  componentDidMount() {
    // ...
    const eventEmitter = new NativeEventEmitter(NativeModules.ToastExample);
    this.eventListener = eventEmitter.addListener('EventReminder', (event) => {
       console.log(event.eventProperty) // "someValue"
       this.setState({name:event.eventProperty });
       console.log(123);
       
    });
    // ...
  }
  componentWillUnmount() {
    this.eventListener.remove(); // 组件卸载时记得移除监听事件
  }

  render() {
    return (
      <View>
        <Text>
          I am {this.state.name}, and I am
          {this.state.isHungry ? " hungry" : " full"}!
        </Text>
        <Button
          onPress={() => {
            this.setState({ isHungry: false });
          }}
          disabled={!this.state.isHungry}
          title={
            this.state.isHungry ? "Pour me some milk, please!" : "Thank you!"
          }
        />
      </View>
    );
  }
}

export default  DeviceMessage;